package com.zkthink.ceres.applet.comment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zkthink.base.R;
import com.zkthink.ceres.applet.comment.domain.vo.AddOrderCommonVo;
import com.zkthink.ceres.applet.comment.domain.vo.AddServerOrderCommentsVo;
import com.zkthink.ceres.applet.common.domin.PageDate;
import com.zkthink.ceres.applet.order.domain.entity.AppletProductOrder;
import com.zkthink.ceres.applet.order.domain.entity.AppletServerOrder;
import com.zkthink.ceres.applet.order.mapper.AppletProductOrderMapper;
import com.zkthink.ceres.applet.order.mapper.AppletServerOrderMapper;
import com.zkthink.ceres.applet.server.domain.vo.PageDateCommentVo;
import com.zkthink.ceres.order.vo.comment.MyCommentVo;
import com.zkthink.context.BaseContextHandler;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zkthink.ceres.applet.comment.mapper.AppletCommentMapper;
import com.zkthink.ceres.applet.comment.domain.entity.AppletComment;
import com.zkthink.ceres.applet.comment.service.AppletCommentService;

import static com.zkthink.ceres.applet.authority.config.AuthorityWebConfiguration.getMid;

/**
 * @Author cmz
 * @Date 2024-04-28 17:33
 */
@Service
public class AppletCommentServiceImpl extends ServiceImpl<AppletCommentMapper, AppletComment> implements AppletCommentService {

    @Resource
    private AppletCommentMapper appletCommentMapper;

    @Resource
    private AppletServerOrderMapper appletServerOrderMapper;

    @Resource
    private AppletProductOrderMapper appletProductOrderMapper;

    @Override
    public R findOneCommentById(PageDateCommentVo pageDateCommentVo) {
        QueryWrapper<AppletComment> queryWrapper = new QueryWrapper<>();
        //产品/服务id
        if (pageDateCommentVo.getProductId() != null) {
            queryWrapper.eq("product_id", pageDateCommentVo.getProductId());
        }
        //产品/服务类型
        if (pageDateCommentVo.getOrderType() != null) {
            queryWrapper.eq("product_type", pageDateCommentVo.getOrderType());
        }
        //店铺
        if (pageDateCommentVo.getStoreId() != null) {
            queryWrapper.eq("store_id", pageDateCommentVo.getStoreId());
        }
        //车惠龙订单id
        if (pageDateCommentVo.getChlorderId() != null) {
            queryWrapper.eq("chlorder_id", pageDateCommentVo.getChlorderId());
        }
        queryWrapper.eq("biz_type", 1);
        List<AppletComment> appletComments = appletCommentMapper.selectList(queryWrapper);
        return R.success(appletComments);
    }

 /*   @Override
    public R addServerOrderComments(AddServerOrderCommentsVo commentsVo) {
        if ((commentsVo.getOrderType()!=0) && (commentsVo.getOrderType()!=1)) {
            return R.fail("订单类型错误");
        }
        AppletComment appletComment = new AppletComment();
        //appletComment.setId(UUID.randomUUID().getMostSignificantBits());
        appletComment.setMid(BaseContextHandler.getUserId());//当前登录用户的id


        
        appletComment.setDescStar(commentsVo.getDescStar());
        appletComment.setLogisticsStar(commentsVo.getLogisticsStar());
        appletComment.setServiceStar(commentsVo.getServiceStar());
        appletComment.setCommentStar(commentsVo.getCommentStar());
        appletComment.setCommentText(commentsVo.getCommentText());
        appletComment.setBizType(commentsVo.getBizType());
        appletComment.setCommentImgs(commentsVo.getCommentImgs());
        appletComment.setLikeNum(commentsVo.getLikeNum());
        appletComment.setProductType(commentsVo.getOrderType());

        QueryWrapper queryWrapper;
        if (commentsVo.getOrderType()==1) {
            //产品订单评论
            queryWrapper = new QueryWrapper<AppletProductOrder>();
            queryWrapper.eq("id", commentsVo.getChlorderId());
            AppletProductOrder productOrder = appletProductOrderMapper.selectOne(queryWrapper);
            
            if (productOrder.getProductStatus() == 1 || productOrder.getProductStatus() == 2) {
                return R.fail("订单未确认收货!!!");
            }
            productOrder.setProductStatus(4);//修改订单状态为已评论
            appletProductOrderMapper.updateById(productOrder);
            
            appletComment.setProductId(productOrder.getProductId());
            appletComment.setStoreId(productOrder.getStoreId());
            appletComment.setVehiclesId(productOrder.getVehiclesId());
            appletComment.setChlorderId(productOrder.getId());
            
            
        } else if (commentsVo.getOrderType()==0) {
            //服务订单评论
            queryWrapper = new QueryWrapper<AppletServerOrder>();
            queryWrapper.eq("id", commentsVo.getChlorderId());
            AppletServerOrder serverOrder = appletServerOrderMapper.selectOne(queryWrapper);

            appletComment.setProductId(serverOrder.getServerId());
            appletComment.setStoreId(serverOrder.getStoreId());
            appletComment.setVehiclesId(serverOrder.getVehiclesId());
            appletComment.setChlorderId(serverOrder.getId());
            

        }
        
        int insert = appletCommentMapper.insert(appletComment);
        return insert > 0 ? R.success() : R.fail("添加失败!!!");
    }*/

    @Override
    public R addOrderComments(AddOrderCommonVo commentsVo) {
        if ((commentsVo.getOrderType()!=0) && (commentsVo.getOrderType()!=1)) {
            return R.fail("订单类型错误");
        }
        AppletComment appletComment = new AppletComment();
        appletComment.setChlorderId(commentsVo.getChlorderId());
        appletComment.setProductType(commentsVo.getOrderType());
        appletComment.setCommentText(commentsVo.getCommentText());
        appletComment.setCommentStar(commentsVo.getCommentStar());
        appletComment.setServiceStar(commentsVo.getServiceStar());
        appletComment.setHideFlag(commentsVo.getHideFlag());
        appletComment.setMid(getMid());
        appletComment.setCreateUser(String.valueOf(getMid()));
        appletComment.setCreateTime(new Date());
        
        QueryWrapper queryWrapper;
        if (commentsVo.getOrderType()==1) {
            //产品订单评论
            queryWrapper = new QueryWrapper<AppletProductOrder>();
            queryWrapper.eq("id", commentsVo.getChlorderId());
            AppletProductOrder productOrder = appletProductOrderMapper.selectOne(queryWrapper);

            if (productOrder.getProductStatus() == 1 || productOrder.getProductStatus() == 2) {
                return R.fail("订单未确认收货!!!");
            }
            productOrder.setProductStatus(4);//修改订单状态为已评论
            appletProductOrderMapper.updateById(productOrder);

            appletComment.setProductId(productOrder.getProductId());
            appletComment.setStoreId(productOrder.getStoreId());
            appletComment.setVehiclesId(productOrder.getVehiclesId());
            appletComment.setChlorderId(productOrder.getId());


        } else if (commentsVo.getOrderType()==0) {
            //服务订单评论
            queryWrapper = new QueryWrapper<AppletServerOrder>();
            queryWrapper.eq("id", commentsVo.getChlorderId());
            AppletServerOrder serverOrder = appletServerOrderMapper.selectOne(queryWrapper);
            if (serverOrder.getServerStatus() != 6) {
                return R.fail("服务订单未签字!!!");
            }
            serverOrder.setServerStatus(7);
            appletServerOrderMapper.updateById(serverOrder);
            
            appletComment.setProductId(serverOrder.getServerId());
            appletComment.setStoreId(serverOrder.getStoreId());
            appletComment.setVehiclesId(serverOrder.getVehiclesId());
            appletComment.setChlorderId(serverOrder.getId());
        }

        int insert = appletCommentMapper.insert(appletComment);
        return insert > 0 ? R.success() : R.fail("添加失败!!!");
    }

    @Override
    public R<List<MyCommentVo>> findMyComment() {
        Long mid = getMid();
        List<MyCommentVo> list = appletCommentMapper.findMyComment(mid);
        return R.success(list);
    }

    @Override
    public R deleteById(Long id) {
        int i = appletCommentMapper.deleteById(id);
        return i>0?R.success():R.fail("删除失败");
    }
}


